/* ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
   Module Name          : com.yaodian100.ec.cn360.remote.RemoteCn360ProductServiceImpl
   Module Description   :

   Date Created      : 2011/6/21
   Original Author   : jeffma
   Team              : yaodian100
   ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
   MODIFICATION HISTORY
   ------------------------------------------------------------------------------
   Date Modified       Modified by       Comments
   ------------------------------------------------------------------------------
   ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
package com.yaodian100.ec.cn360.remote;

import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;

import com.yaodian100.core.thread.service.ThreadService;
import com.yaodian100.ec.category.service.CategoryService;
import com.yaodian100.ec.cn360.batch.BuildCn360BrandJob;
import com.yaodian100.ec.cn360.batch.BuildCn360CatsJob;
import com.yaodian100.ec.cn360.batch.BuildCn360PriceJob;
import com.yaodian100.ec.cn360.batch.BuildCn360ProductJob;
import com.yaodian100.ec.cn360.batch.BuildCn360PropsJob;
import com.yaodian100.ec.product.service.ProductService;

/**
 * @author jeffma
 * 
 */
public class RemoteCn360ProductServiceImpl implements RemoteCn360ProductService {

	protected final Logger logger = LoggerFactory.getLogger(getClass());

	@Autowired
	private ThreadService threadService;
	@Autowired
	private ProductService productService;
	@Autowired
	private CategoryService categoryService;
	@Autowired
	private JdbcTemplate jdbcTemplate;

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.yaodian100.ec.cn360.remote.RemoteCn360ProductService#buildFeed()
	 */
	@Override
	public boolean buildAll() {
		List<Map<String, Object>> products = getProducts();
		logger.info("build product size:" + products.size());
		buildProduct(products);
		buildPrice(products);
		buildCats();
		buildBrand();
		buildProps(products);
		return true;
	}

	public boolean buildProduct(List<Map<String, Object>> products) {
		if (products == null) {
			products = getProducts();
		}
		threadService.executeInBackground(new BuildCn360ProductJob(productService, products));
		return true;
	}

	public boolean buildProps(List<Map<String, Object>> products) {
		if (products == null) {
			products = getProducts();
		}
		threadService.executeInBackground(new BuildCn360PropsJob(productService, products));
		return true;
	}

	public boolean buildPrice(List<Map<String, Object>> products) {
		if (products == null) {
			products = getProducts();
		}
		threadService.executeInBackground(new BuildCn360PriceJob(products));
		return true;
	}

	public boolean buildCats() {
		threadService.executeInBackground(new BuildCn360CatsJob(categoryService));
		return true;
	}

	public boolean buildBrand() {
		threadService.executeInBackground(new BuildCn360BrandJob(productService));
		return true;
	}

	private List<Map<String, Object>> getProducts() {
		StringBuffer sql = new StringBuffer();
		sql.append("select pm.product_id,pm.product_name,pm.modify_date product_modify,vs.modify_date shelf_modify,vs.SHELF_PRICE,vs.PROMOTE_NAME,vs.START_DATE SHELF_START_DATE, pm.product_brand, pm.main_category_id,pm.MAIN_CATEGORY_PATH,pm.list_price, s.join_spec, pm.MAIN_CODE_LABEL, pm.SECOND_CODE_LABEL, pm.MAIN_SPEC_NAME, pm.SECOND_SPEC_NAME");
		sql.append(" from ec_product_main pm");
		sql.append(" left join ec_view_shelf vs on vs.PRODUCT_ID = pm.PRODUCT_ID and sysdate between vs.start_date and vs.end_date and vs.shelf_status='SHF500'");
		sql.append(" left join (select product_id, sum(available_inventory+cancel_inventory-sold_inventory) inventory from ec_specification_inventory group by product_id) si on si.product_id=pm.PRODUCT_ID");
		sql.append(" left join (select product_id, tab_to_string(CAST(COLLECT(DISTINCT SPECIFICATION_NAME) AS t_varchar2_tab)) join_spec from ec_specification where is_delete='N' group by product_id) s on s.product_id= pm.PRODUCT_ID");
		sql.append(" where vs.SHELF_PRICE is not null and pm.product_cost is not null and vs.SHELF_PRICE>0");
		sql.append(" and (pm.is_delete='N' or pm.is_delete='Q') and pm.product_type='0' and si.inventory > ");
		sql.append(2);
		sql.append(" and (vs.SHELF_PRICE-pm.product_cost)/vs.SHELF_PRICE > ");
		sql.append(0.01);
		// sql.append(" and rownum<=100");
		sql.append(" order by pm.product_id asc");
		List<Map<String, Object>> products = jdbcTemplate.queryForList(sql.toString());
		return products;
	}
}
